package com.store.bauhinia.order.controller;

import com.bookstore.bauhinia.pojo.CommonResult;
import com.bookstore.bauhinia.pojo.Order;
import com.store.bauhinia.order.service.OrderService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class OrderController {
    @Resource
    private OrderService orderService;

    @PostMapping("/order/insert")
    public CommonResult insertOrder(@RequestBody Order record) {
        int result = orderService.insert(record);
        System.out.println("*****插入结果:" + result);
        if (result > 0) {
            return new CommonResult(200, "插入数据库成功", record);
        } else {
            return new CommonResult(444, "插入数据库失败", null);
        }
    }

    @GetMapping("/order/get/{id}")
    public CommonResult getOrderById(@PathVariable("id") int id) {
        Order order = orderService.selectByPrimaryKey(id);
        System.out.println("*****查询结果:" + order);
        if (order != null) {
            return new CommonResult(200, "查询成功", order);
        } else {
            return new CommonResult(444, "没有对应记录,查询id:" + id, null);
        }
    }

    @GetMapping("/order/delete/{id}")
    public CommonResult deleteOrderById(@PathVariable("id") int id) {
        int result = orderService.deleteByPrimaryKey(id);
        if (result > 0) {
            return new CommonResult(200, "删除数据成功");
        } else {
            return new CommonResult(444, "删除数据失败");
        }
    }

    @PostMapping("/order/update")
    public CommonResult updateOrder(@RequestBody Order record) {
        int result = orderService.updateByPrimaryKey(record);
        if (result > 0) {
            return new CommonResult(200, "更新数据成功", record);
        } else {
            return new CommonResult(444, "更新数据失败", null);
        }
    }

    @GetMapping("/order/all")
    public CommonResult getAllOrders(){
        List<Order> orders = orderService.getAllOrders();
        if(orders != null){
            return new CommonResult(200, "查询数据成功", orders);
        } else {
            return new CommonResult(444, "查询数据失败", null);
        }
    }

    @GetMapping("/order/getByUserId/{userId}")
    public CommonResult getOrdersByUserId(@PathVariable Integer userId){
        List<Order> orders = orderService.getOrdersByUserId(userId);
        if(orders != null){
            return new CommonResult(200, "查询数据成功", orders);
        } else {
            return new CommonResult(444, "查询数据失败", null);
        }
    }
}
